//
//  NDBaseContentView.swift
//  NyamanDana
//
//  Created by 李永彬 on 2025/10/4.
//

import SwiftUI

// 定义通知名称
extension Notification.Name {
    static let updateTabBarVisibility = Notification.Name("updateTabBarVisibility")
}

// 定义通知键
struct TabBarVisibilityKeys {
    static let isHidden = "isHidden"
}

struct NDBaseConfig {
    var backgroundImgName: String = "sign up background1"
    var backgroundColor: Color = Color.white
    var padding: EdgeInsets = EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)
    var showNavigationBar: Bool = true
    var safeAreaIgnored: Bool = true
    var hiddenTabbar: Bool = true // 控制是否隐藏 tabbar
}

struct NDBaseContentView<Content: View>: View {
    let content: Content
    var baseConfig: NDBaseConfig
    var navigationConfig: NavigationConfig
    
    @Environment(\.presentationMode) var presentationMode
    @State private var isLoading: Bool = false
    
    init(baseConfig: NDBaseConfig = NDBaseConfig(),
         navigationConfig: NavigationConfig = NavigationConfig(title: ""),
         @ViewBuilder content: () -> Content) {
        self.baseConfig = baseConfig
        self.navigationConfig = navigationConfig
        self.content = content()
    }
    
    var body: some View {
        ZStack {
            // 背景
            baseConfig.backgroundColor
                .edgesIgnoringSafeArea(baseConfig.safeAreaIgnored ? .all : [])
            backgroundImage
            // 主要内容
            content
                .padding(baseConfig.padding)
        }
        .modifier(NDCustomNavigationModifier(config: navigationConfig))
        .navigationBarHidden(!baseConfig.showNavigationBar)
        .onAppear() {
            updateTabBarVisibility()
        }
        .onDisappear {
            // 页面消失时立即恢复 TabBar
            if baseConfig.hiddenTabbar {
                showTabBar()
            }
        }
    }
    
    // 更新 TabBar 可见性
    private func updateTabBarVisibility() {
        if baseConfig.hiddenTabbar {
            hideTabBar()
        } else {
            showTabBar()
        }
    }
    
    // 隐藏 TabBar
    private func hideTabBar() {
        NotificationCenter.default.post(
            name: .updateTabBarVisibility,
            object: nil,
            userInfo: [TabBarVisibilityKeys.isHidden: true]
        )
    }
    
    // 显示 TabBar
    private func showTabBar() {
        NotificationCenter.default.post(
            name: .updateTabBarVisibility,
            object: nil,
            userInfo: [TabBarVisibilityKeys.isHidden: false]
        )
    }
    
    // 背景图片
    private var backgroundImage: some View {
        Group {
            GeometryReader { geometry in
                Image(baseConfig.backgroundImgName)
                    .resizable()
                    .aspectRatio(contentMode: .fill)
                    .frame(width: geometry.size.width)
                    .frame(width: geometry.size.width, alignment: .top)
                    .clipped()
            }
            .edgesIgnoringSafeArea(.all)
        }
    }
}
